﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace test
{
    public partial class FormRoomSort : Form
    {
        DataSet ds = new DataSet();
        public FormRoomSort()
        {
            InitializeComponent();
        }
        //窗体加载事件
        private void FormRoomSort_Load(object sender, EventArgs e)
        {
            GetSort();
        }
        //得到全部的房屋类型
        private void GetSort()
        {
            DBHelper dBHelper = new DBHelper();
            //删除之前的，重新创建
            ds = new DataSet();
            try
            {
                //编写sql语句
                String sql = "select id 编号, roomsize 房屋类别 from roomsize order by id";
                //创建适配器对象
                MySqlDataAdapter adapter = new MySqlDataAdapter(sql, dBHelper.Connection);
                //填充数据集
                adapter.Fill(ds, "RoomSize");
                //数据绑定
                this.dgv_RoomSort.DataSource = this.ds.Tables["RoomSize"];
            }catch(Exception e)
            {
                MessageBox.Show("获取房屋类别失败！" + e, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        //退出事件
        private void btn_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //删除操作
        private void btn_Delete_Click(object sender, EventArgs e)
        {
            DBHelper dBHelper = new DBHelper();
            try
            {
                if(this.dgv_RoomSort.CurrentRow!=null) //判断是否选中一行进行删除
                {
                    //创建动态sql语句
                    StringBuilder sb = new StringBuilder();
                    sb.AppendFormat("确定要删除类别为{0}的数据吗?", this.dgv_RoomSort.CurrentRow.Cells[1].Value);
                    DialogResult result = MessageBox.Show(sb.ToString(), "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                    //判断是否点击确定
                    if (result == DialogResult.Yes)
                    {
                        //重新初始化，清空之前的字符串
                        sb = new StringBuilder();
                        //创建sql语句
                        sb.AppendFormat("delete from RoomSize where id = {0}", Convert.ToInt32(this.dgv_RoomSort.CurrentRow.Cells[0].Value));
                        //创建执行工具
                        MySqlCommand cmd = new MySqlCommand(sb.ToString(), dBHelper.Connection);
                        //打开连接
                        dBHelper.OpenConnection();
                        //执行
                        int rs = cmd.ExecuteNonQuery();
                        //判断是否执行成功
                        if (rs == 1)
                        {
                            MessageBox.Show("删除成功！","系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            this.GetSort();
                        }
                    }
                }
                else
                {
                    MessageBox.Show("未选中！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("删除失败！" + ex, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                dBHelper.CloseConnection();
            }
        }
        //增加
        private void btn_Add_Click(object sender, EventArgs e)
        {
            FormRoomSizeSort form = new FormRoomSizeSort();
            form.ShowDialog();
            //增加后再次刷新列表
            this.GetSort();
        }
        //修改
        private void btn_Update_Click(object sender, EventArgs e)
        {
            FormRoomSizeSort form = new FormRoomSizeSort();
            //获取当前选中的ID
            form.SortID = Convert.ToInt32(this.dgv_RoomSort.CurrentRow.Cells[0].Value);
            form.ShowDialog();
            //增加后再次刷新列表
            this.GetSort();
        }
    }
}
